<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="题目类型" prop="subjectType">
      <el-input v-model="dataForm.subjectType" placeholder="题目类型"></el-input>
    </el-form-item>
    <el-form-item label="题目难度" prop="subjectDifficlty">
      <el-input v-model="dataForm.subjectDifficlty" placeholder="题目难度"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="titleName">
      <el-input v-model="dataForm.titleName" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="标准答案" prop="standardAnswer">
      <el-input v-model="dataForm.standardAnswer" placeholder="标准答案"></el-input>
    </el-form-item>
    <el-form-item label="时间限制" prop="timeLimit">
      <el-input v-model="dataForm.timeLimit" placeholder="时间限制"></el-input>
    </el-form-item>
    <el-form-item label="题目描述" prop="subiectDesc">
      <el-input v-model="dataForm.subiectDesc" placeholder="题目描述"></el-input>
    </el-form-item>
    <el-form-item label="是否启动" prop="isStart">
      <el-input v-model="dataForm.isStart" placeholder="是否启动"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="beginData">
      <el-input v-model="dataForm.beginData" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endData">
      <el-input v-model="dataForm.endData" placeholder="结束时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          subjectType: '',
          subjectDifficlty: '',
          titleName: '',
          standardAnswer: '',
          timeLimit: '',
          subiectDesc: '',
          isStart: '',
          beginData: '',
          endData: ''
        },
        dataRule: {
          subjectType: [
            { required: true, message: '题目类型不能为空', trigger: 'blur' }
          ],
          subjectDifficlty: [
            { required: true, message: '题目难度不能为空', trigger: 'blur' }
          ],
          titleName: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          standardAnswer: [
            { required: true, message: '标准答案不能为空', trigger: 'blur' }
          ],
          timeLimit: [
            { required: true, message: '时间限制不能为空', trigger: 'blur' }
          ],
          subiectDesc: [
            { required: true, message: '题目描述不能为空', trigger: 'blur' }
          ],
          isStart: [
            { required: true, message: '是否启动不能为空', trigger: 'blur' }
          ],
          beginData: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endData: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/reply/subject/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.subjectType = data.subject.subjectType
                this.dataForm.subjectDifficlty = data.subject.subjectDifficlty
                this.dataForm.titleName = data.subject.titleName
                this.dataForm.standardAnswer = data.subject.standardAnswer
                this.dataForm.timeLimit = data.subject.timeLimit
                this.dataForm.subiectDesc = data.subject.subiectDesc
                this.dataForm.isStart = data.subject.isStart
                this.dataForm.beginData = data.subject.beginData
                this.dataForm.endData = data.subject.endData
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/reply/subject/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'subjectType': this.dataForm.subjectType,
                'subjectDifficlty': this.dataForm.subjectDifficlty,
                'titleName': this.dataForm.titleName,
                'standardAnswer': this.dataForm.standardAnswer,
                'timeLimit': this.dataForm.timeLimit,
                'subiectDesc': this.dataForm.subiectDesc,
                'isStart': this.dataForm.isStart,
                'beginData': this.dataForm.beginData,
                'endData': this.dataForm.endData
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
